package com.zat.mes.andrmes.dofun.work.vm

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.*
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import okhttp3.RequestBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Response

/**
 * @author zy
 * @date :2020-02-17 16:53
 * @description: 审核 取消审核model
 */
class WorkRecordViewModel(application: Application) : AndroidViewModel(application) {

    private val empListData: MutableLiveData<MutableList<EmpBeanItem>> = MutableLiveData()
    private val workRecordData: MutableLiveData<MutableList<DayWorkRecordBean>> =
        MutableLiveData()

    private val workRecord: MutableLiveData<MutableList<DayWorkRecordBean>> = MutableLiveData()
    private val pieceInfo: MutableLiveData<DayWorkRecordBean> = MutableLiveData()

    private val packData: MutableLiveData<PackBean> = MutableLiveData()
    private val insertRecordData: MutableLiveData<WorkRecordInsertResult> = MutableLiveData()
    private val insertRecordNoPlan: MutableLiveData<String> = MutableLiveData()
    private val bindClothes: MutableLiveData<String> = MutableLiveData()

    private val messageData: MutableLiveData<String> = MutableLiveData()
    fun getPieceInfo(): MutableLiveData<DayWorkRecordBean> {
        return pieceInfo
    }
    fun getMessageData(): MutableLiveData<String> {
        return messageData
    }
    fun getEmpList(): MutableLiveData<MutableList<EmpBeanItem>> {
        return empListData
    }

    fun getWorkRecordData(): MutableLiveData<MutableList<DayWorkRecordBean>> {
        return workRecordData
    }

    fun getWorkRecord(): MutableLiveData<MutableList<DayWorkRecordBean>> {
        return workRecord
    }

    fun getPackData(): MutableLiveData<PackBean> {
        return packData
    }

    fun getRecordData(): MutableLiveData<WorkRecordInsertResult> {
        return insertRecordData
    }

    fun getNoPlanWorkResult(): MutableLiveData<String> {
        return insertRecordNoPlan;
    }

    fun getBindClothes():MutableLiveData<String>{
        return bindClothes
    }
    /**
     * 获取每个班组下的人员列表
     */
    fun loadClassEmpList(currentPage: Int, pageSize: Int, classId: Long, day: String) {
        RetrofitManager.apiService(ApiService::class.java)
            .getClassEmpList(currentPage, pageSize, classId)
            .enqueue(object : MesCallBack<BaseResponse<EmpBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<EmpBean>>?,
                    response: Response<BaseResponse<EmpBean>>?
                ) {
                    val result = response?.body()?.result
                    loadDayPlayEmpList(classId, day, result?.items)
                }

            })
    }

    /**
     * 查询日生产计划人员
     */
    fun loadDayPlayEmpList(
        classId: Long,
        day: String,
        items: List<EmpBeanItem>?
    ) {
        RetrofitManager.apiService(ApiService::class.java)
            .getWorkRecordEmpList(classId, day)
            .enqueue(object : MesCallBack<BaseResponse<List<DayWorkRecordBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<DayWorkRecordBean>>>?,
                    response: Response<BaseResponse<List<DayWorkRecordBean>>>?
                ) {
                    val result = response?.body()?.result
                    if (items != null && result != null) {
                        for (i in items?.indices) {
                            for (k in result?.indices) {
                                if (items?.get(i).empid == result?.get(k).empId) {
                                    items?.get(i).quantity = result?.get(k).quantity
                                }
                            }
                        }
                    }
                    empListData.postValue(items as MutableList<EmpBeanItem>?)
                }

            })
    }

    /**
     * 查询日生产记录
     */
    fun loadDayRecordList(empId: Long, day: String) {
        RetrofitManager.apiService(ApiService::class.java)
            .getWorkRecordList(empId, day)
            .enqueue(object : MesCallBack<BaseResponse<List<DayWorkRecordBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<DayWorkRecordBean>>>?,
                    response: Response<BaseResponse<List<DayWorkRecordBean>>>?
                ) {
                    val result = response?.body()?.result
                    workRecordData.postValue(result as MutableList<DayWorkRecordBean>?)
                }

            })

    }

    /**
     * 查询日生产记录
     */
    fun loadDayRecord(empId: Long, packId: Long, orderId: Long) {
        RetrofitManager.apiService(ApiService::class.java)
            .getWorkRecord(empId, packId, orderId)
            .enqueue(object : MesCallBack<BaseResponse<List<DayWorkRecordBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<DayWorkRecordBean>>>?,
                    response: Response<BaseResponse<List<DayWorkRecordBean>>>?
                ) {
                    val result = response?.body()?.result
                    workRecord.postValue(result as MutableList<DayWorkRecordBean>?)
                }

            })

    }

    /**
     * 查询日生产记录 最近一次
     */
    fun loadDayRecordLast(empId: Long) {
        RetrofitManager.apiService(ApiService::class.java)
            .getWorkRecordLast(empId)
            .enqueue(object : MesCallBack<BaseResponse<List<DayWorkRecordBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<DayWorkRecordBean>>>?,
                    response: Response<BaseResponse<List<DayWorkRecordBean>>>?
                ) {
                    val result = response?.body()?.result
                    workRecord.postValue(result as MutableList<DayWorkRecordBean>?)
                }

            })

    }

    /**
     * 根据二维码查询包(做工)
     */
    fun loadPackData(qrCode: String?) {
        RetrofitManager.apiService(ApiService::class.java)
            .getPackByQrcode(qrCode)
            .enqueue(object : MesCallBack<BaseResponse<PackBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<PackBean>>?,
                    response: Response<BaseResponse<PackBean>>?
                ) {
                    val result = response?.body()?.result
                    packData.postValue(result)
                }

            })
    }



    /**
     * 根据二维码查询包（质检）
     */
    fun loadPackDataQc(qrCode: String?) {
        RetrofitManager.apiService(ApiService::class.java)
            .getPackByQrcodeQc(qrCode)
            .enqueue(object : MesCallBack<BaseResponse<PackBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<PackBean>>?,
                    response: Response<BaseResponse<PackBean>>?
                ) {
                    val result = response?.body()?.result
                    packData.postValue(result)
                }

            })
    }
    /**
     * 根据二维码查询包
     */
    fun loadPackDataUpdate(qrCode: String?) {
        RetrofitManager.apiService(ApiService::class.java)
            .getPackByQrcodeUpdate(qrCode)
            .enqueue(object : MesCallBack<BaseResponse<PackBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<PackBean>>?,
                    response: Response<BaseResponse<PackBean>>?
                ) {
                    val result = response?.body()?.result
                    packData.postValue(result)
                }

            })
    }


    /**
     * 根据卡查询包(做工)
     */
    fun loadPackDataByRFRD(rfId: String?) {
        RetrofitManager.apiService(ApiService::class.java)
            .getPackByRFRD(rfId)
            .enqueue(object : MesCallBack<BaseResponse<PackBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<PackBean>>?,
                    response: Response<BaseResponse<PackBean>>?
                ) {
                    val result = response?.body()?.result
                    packData.postValue(result)
                }

            })
    }

    /**
     * 根据卡查询包(质检)
     */
    fun loadPackDataByRFRDQc(rfId: String?) {
        RetrofitManager.apiService(ApiService::class.java)
            .getPackByRFRDQc(rfId)
            .enqueue(object : MesCallBack<BaseResponse<PackBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<PackBean>>?,
                    response: Response<BaseResponse<PackBean>>?
                ) {
                    val result = response?.body()?.result
                    packData.postValue(result)
                }

            })
    }


    /**
     * 根据二维码查询包
     */
    fun loadPackDataByRFRDUpdate(rfId: String?) {
        RetrofitManager.apiService(ApiService::class.java)
            .getPackByRFRDUpdate(rfId)
            .enqueue(object : MesCallBack<BaseResponse<PackBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<PackBean>>?,
                    response: Response<BaseResponse<PackBean>>?
                ) {
                    val result = response?.body()?.result
                    packData.postValue(result)
                }

            })
    }


    /**
     * 新增日生产计划记录
     */
    fun loadRecordData(param: String) {

        val requestBody: RequestBody = RequestBody.create(RetrofitManager.JOSNTYPE, param)
        RetrofitManager.apiService(ApiService::class.java)
            .insertRecord(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<WorkRecordInsertResult>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<WorkRecordInsertResult>>?,
                    response: Response<BaseResponse<WorkRecordInsertResult>>?
                ) {
                    if (response?.body()?.success != null) {
                        if (!response?.body()?.success!!) {
                            messageData.postValue( response?.body()?.message)

                            return
                        }
                    }

                    val workRecordInsertResult = response?.body()?.result
                    insertRecordData.postValue(workRecordInsertResult)

                }

            })
    }

    //POST /work/workrecord/findPieceInfo
    fun findPieceInfo(param:String){

//        val requestBody: RequestBody = RequestBody.create(RetrofitManager.JOSNTYPE, param)
//        RetrofitManager.apiService(ApiService::class.java)

        RetrofitManager.apiService(ApiService::class.java)
            .findPieceInfo(param)
            .enqueue(object : MesCallBack<BaseResponse<DayWorkRecordBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<DayWorkRecordBean>>?,
                    response: Response<BaseResponse<DayWorkRecordBean>>?
                ) {
                    if (response?.body()?.success != null) {
                        if (!response?.body()?.success!!) {
                            messageData.postValue( response?.body()?.message)
                            return
                        }
                    }

                    val workRecordInsertResult = response?.body()?.result
                    pieceInfo.postValue(workRecordInsertResult)

                }

            })
    }

    fun saveWork(procId:String,publicQRCode: String) {

        val jsonObject = JSONObject()
        jsonObject.put("procId", procId);
        jsonObject.put("publicQRCode", publicQRCode)
        val requestBody: RequestBody = RequestBody.create(RetrofitManager.JOSNTYPE, jsonObject.toString())
        RetrofitManager.apiService(ApiService::class.java)
            .saveWork(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<Object>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<Object>>?,
                    response: Response<BaseResponse<Object>>?
                ) {

                    val workRecordInsertResult = response?.body()?.message
                    //insertRecordData.postValue(workRecordInsertResult)
                    messageData.postValue( response?.body()?.message)


                }

            })
    }


    /**
     * 无日计划的做工
     */
    fun loadNoPlanWork(param: String) {

        val requestBody: RequestBody =
            RequestBody.create(RetrofitManager.JOSNTYPE, param)
        RetrofitManager.apiService(ApiService::class.java)
            .workNoPlan(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<WorkRecordInsertResult>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<WorkRecordInsertResult>>?,
                    response: Response<BaseResponse<WorkRecordInsertResult>>?
                ) {
                    if (response?.body()?.success != null) {
                        if (response?.body()?.success!!) {
                            insertRecordNoPlan.postValue(response?.body()?.message)
                        } else {
                            insertRecordNoPlan.postValue(getApplication<Application>().getString(R.string.text_insert_fail))
                        }
                    } else {
                        insertRecordNoPlan.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                    }
                }

            })
    }

    /**
     * 扫码绑衣服
     */
    fun bindClothes(param:String){
        val requestBody: RequestBody =
            RequestBody.create(RetrofitManager.JOSNTYPE, param)
        RetrofitManager.apiService(ApiService::class.java)
            .postClothesBind(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    bindClothes.postValue(response?.body()?.message)
                }

            })
    }

}